package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiNiuUtils;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import org.slf4j.Logger;

import javax.print.DocFlavor;

/**
 * @program: health_parent
 * @description: 套餐controler
 * @author: HuChunping
 * @create: 2021-01-09 22:10
 **/
@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    private static final Logger log = LoggerFactory.getLogger(SetMealController.class);

    /**
     * 远程调用SetmealService
     */
    @Reference
    private SetmealService setmealService;

    /** 
    * @Description:  图片上传,
     *               图片上传放controller层，不放业务层原因：不需要访问数据库，controller与业务层已经解耦了.
     *               如果业务层处理，需要额外经过一层网络远程调用，不推荐
    * @Param: [imgFile] 
    * @return: com.itheima.health.entity.Result 
    * @Author: HuChunping 
    * @Date: 2021/1/9-22:21
    */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        // 获取源文件名
        String originalFilename = imgFile.getOriginalFilename();
        // 截取后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 生成唯一id
        String uniqueId = UUID.randomUUID().toString();
        // 拼接唯一文件名
        String filename = uniqueId + suffix;
        // 调用七牛工具上传图片 //uploadFile方法为上传本地文件，所以这里使用字节流方法
        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), filename);
            HashMap<String, String> map = new HashMap<>(2);
            map.put("imgName", filename);
            map.put("domain", QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,map);
        } catch (IOException e) {
            log.error("上传文件失败");
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
    * @Description: 新增检查套餐
    * @Param: [setmeal, checkgroupIds]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/11-11:15
    */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        setmealService.add(setmeal, checkgroupIds);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
    * @Description: 检查套餐的分页查询
    * @Param: [queryPageBean]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/11-11:15
    */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Setmeal> page = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, page);
    }

    /**
    * @Description: 通过id查询套餐
    * @Param: [id]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/11-14:47
    */
    @GetMapping("/findById")
    public Result findById(int id){
        Setmeal setmeal = setmealService.findById(id);
        // 创建map放入套餐信息和域名
        HashMap<String, Object> map = new HashMap<>(4);
        map.put("setmeal", setmeal);
        map.put("domain",QiNiuUtils.DOMAIN);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, map);
    }

    /**
    * @Description: 通过套餐id查询勾选的检查组
    * @Param: [id]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/11-14:51
    */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(int id){
        List<Integer> checkGroupIds = setmealService.findCheckGroupIdsBySetmealId(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIds);
    }

    /**
    * @Description: 更新检查套餐
    * @Param: [setmeal, checkGroupIds]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/11-15:36
    */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkGroupIds){
        setmealService.update(setmeal, checkGroupIds);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    @PostMapping("/deleteById")
    public Result deleteById(int id){
         setmealService.deleteById(id);
         return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
